/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo3791;

import eu.mpower.framework.management.patientmanagement.soap.GetPatientsForUserMessage;
import eu.mpower.framework.management.patientmanagement.soap.GetPersonForUserMessage;
import eu.mpower.framework.management.patientmanagement.soap.Patient;
import eu.mpower.framework.management.patientmanagement.soap.PatientsResponseMessage;
import eu.mpower.framework.management.patientmanagement.soap.PersonResponseMessage;
import eu.mpower.framework.security.types.soap.LoginStatus;
import eu.mpower.framework.security.types.soap.SecurityToken;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author stalew
 */
public class myFrame extends javax.swing.JFrame {

    /**
     * Creates new form myFrame
     */
    public myFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userName = new javax.swing.JTextField();
        passWord = new javax.swing.JTextField();
        loginButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        logWindow = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        loginButton.setText("Login");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        logWindow.setColumns(20);
        logWindow.setRows(5);
        jScrollPane1.setViewportView(logWindow);

        jLabel1.setText("Username:");

        jLabel2.setText("Password:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(50, 50, 50)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 426, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(layout.createSequentialGroup()
                            .add(jLabel1)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(userName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .add(jLabel2)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(loginButton)
                                .add(passWord)))))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(39, 39, 39)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(userName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(passWord, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(loginButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 87, Short.MAX_VALUE)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 204, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
        // TODO add your handling code here:
        String user = userName.getText().trim();
        String pwd = passWord.getText().trim();
        
        int userID = 0;
        SecurityToken secToken = null;
        
        LoginStatus ls = authenticateUserPass(user,pwd);
        
        if (ls.getStatus().getResult()==0) {
            userID = ls.getSecurityToken().getUserID();
            secToken = ls.getSecurityToken();
            logWindow.append("\nLogin ok:" + ls.getMessage() + " for user " + ls.getSecurityToken().getUserID());
            
            GetPersonForUserMessage gpfum = new GetPersonForUserMessage();
            gpfum.setSecurityToken(secToken);
            gpfum.setUserID(new Long(userID).longValue());
            
            PersonResponseMessage  prm = getPersonForUser(gpfum);
            
            if (prm.getStatus().getResult()==0) {
                logWindow.append("\nGetPersonForUser ok: " + prm.getPerson().getGivenNames() + " " + prm.getPerson().getFamilyName() + " logged in");
            
            }
            
            GetPatientsForUserMessage patM = new GetPatientsForUserMessage();
            patM.setSecurityToken(secToken);
            patM.setUserID(new Long(userID).longValue());
            
            PatientsResponseMessage patRes =  getPatientsForUser(patM);
            
            if (patRes.getStatus().getResult()==0) {
            
                List<Patient> patList = patRes.getPatients();
                
                for (Patient p: patList) {
                    logWindow.append("\nPatient found" + p.getPerson().getFamilyName() + " " + p.getPerson().getGivenNames() + " (" + p.getId() + ")");
                    
                }
                
                
                    
            }
            
            
        JFrame myFrame2 = new subjectScreen();
        myFrame2.setTitle("Calendar");
       
        myFrame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame2.setVisible(true);
        
        this.setVisible(false);
            
            
            
        
        }
        
        
    }//GEN-LAST:event_loginButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(myFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(myFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(myFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(myFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new myFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea logWindow;
    private javax.swing.JButton loginButton;
    private javax.swing.JTextField passWord;
    private javax.swing.JTextField userName;
    // End of variables declaration//GEN-END:variables

    private static LoginStatus authenticateUserPass(java.lang.String username, java.lang.String password) {
        eu.mpower.framework.security.authentication.soap.AuthenticationWServiceService service = new eu.mpower.framework.security.authentication.soap.AuthenticationWServiceService();
        eu.mpower.framework.security.authentication.soap.PortTypeAuthenticationWService port = service.getAuthenticationWServicePort();
        return port.authenticateUserPass(username, password);
    }

    private static PersonResponseMessage getPersonForUser(eu.mpower.framework.management.patientmanagement.soap.GetPersonForUserMessage userIDmsg) {
        eu.mpower.framework.management.patientmanagement.soap.PatientManagement service = new eu.mpower.framework.management.patientmanagement.soap.PatientManagement();
        eu.mpower.framework.management.patientmanagement.soap.IPatientManagement port = service.getIPatientManagement();
        return port.getPersonForUser(userIDmsg);
    }

    private static PatientsResponseMessage getPatientsForUser(eu.mpower.framework.management.patientmanagement.soap.GetPatientsForUserMessage userIDmsg) {
        eu.mpower.framework.management.patientmanagement.soap.PatientManagement service = new eu.mpower.framework.management.patientmanagement.soap.PatientManagement();
        eu.mpower.framework.management.patientmanagement.soap.IPatientManagement port = service.getIPatientManagement();
        return port.getPatientsForUser(userIDmsg);
    }
}
